package com.zhouchb.blog.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhouchb.blog.bean.Blog;
import com.zhouchb.blog.bean.Tag;
import com.zhouchb.blog.service.BlogService;
import com.zhouchb.blog.service.TagsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author Zhouchb
 * @Create 2021-09-2021/9/15 :10:11
 * @Description tag前端页面
 */
@Slf4j
@Controller
public class TagShowController {

    @Autowired
    private TagsService tagsService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/tags/{id}")
    public String showTags(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                           @PathVariable("id")Long id, Model model){
        List<Tag> tags = tagsService.listTagAndCountService();
        if (id==-1){
            if (tags==null) {
                id=0L;
            }else {
                id = tags.get(0).getId();
            }
        }
        PageHelper.startPage(pn,6);
        List<Blog> blogList = blogService.listBlogByTagIdService(id);
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogList, 5);
        model.addAttribute("blogPageInfo",blogPageInfo);
        model.addAttribute("tags",tags);
        model.addAttribute("activeTagId",id);
        return "tags";
    }
}
